package com.hs.sfzy.controller;

import com.hs.sfzy.entity.Appointment;
import com.hs.sfzy.service.AppointmentService;
import com.hs.sfzy.util.Result;
import com.hs.sfzy.util.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @ClassName AppointmentController
 * @Description TODO
 * @Author QinKunPeng
 * @Date 2024年10月24日 19:14
 * @Version 1.0
 */
@RestController
@RequestMapping("/appointment")
public class AppointmentController {

    @Autowired
    AppointmentService appointmentService;

    /**
     * 动态条件获取咨询预约
     * @author QinKunPeng
     * @date 2024/10/24 20:02
     * @param map
     * @return java.lang.Object
     */
    @GetMapping
    public Object getAppointments(@RequestParam(required = false) Map map){
        List<Appointment> appointments = appointmentService.getAppointments(map);
        return ResultUtil.success().setData(appointments);
    }

    /**
     * 分页、动态条件获取咨询预约
     * @author QinKunPeng
     * @date 2024/10/24 20:03
     * @param pageNum
     * @param pageSize
     * @param map
     * @return java.lang.Object
     */
    @GetMapping("/page")
    public Object getAppointmentByPage(
            @RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
            @RequestParam(value = "pageSize", defaultValue = "5") int pageSize,
            @RequestParam(required = false) Map map){
        return ResultUtil.success().setData(appointmentService.getAppointmentByPage(pageNum, pageSize,map));
    }

    /**
     * 根据id获取咨询预约
     * @author QinKunPeng
     * @date 2024/10/24 20:03
     * @param id
     * @return java.lang.Object
     */
    @GetMapping("/{id}")
    public Object getAppointmentById(@PathVariable("id") int id){
        return ResultUtil.success().setData(appointmentService.getAppointment(id));
    }

    /**
     * 根据id删除咨询预约
     * @author QinKunPeng
     * @date 2024/10/24 20:03
     * @param id
     * @return java.lang.Object
     */
    @DeleteMapping("/{id}")
    public Object deleteAppointmentById(@PathVariable("id") int id){
        int num = appointmentService.deleteAppointment(id);
        if (num > 0){
            return ResultUtil.success("删除成功");
        }else{
            return ResultUtil.fail("删除失败");
        }

    }

    /**
     * 添加一条咨询预约
     * @author QinKunPeng
     * @date 2024/10/24 20:03
     * @param appointment
     * @return java.lang.Object
     */
    @PostMapping
    public Object addAppointment(@RequestBody(required = false) Appointment appointment){
        if (appointment == null){
            return ResultUtil.fail("数据有误");
        }else{
            appointment.setCreateTime(new Date());
            int num = appointmentService.addAppointment(appointment);
            if (num > 0){
                return ResultUtil.success("添加成功");
            }else{
                return ResultUtil.fail("添加失败");
            }
        }
    }

    /**
     * 修改咨询预约[咨询师回复使用]
     * @author QinKunPeng
     * @date 2024/10/24 20:04
     * @param appointment
     * @return java.lang.Object
     */
    @PutMapping
    public Object updateAppointment(@RequestBody(required = false) Appointment appointment){
        if (appointment == null){
            return ResultUtil.fail("数据有误");
        }else {
            appointment.setReplyTime(new Date());
            int num = appointmentService.updateAppointment(appointment);
            if (num > 0){
                return ResultUtil.success("更新成功");
            }
            return ResultUtil.fail("更新失败");
        }
    }


    @GetMapping("/findAllY")
    public Result findAllY(){
        List<Appointment> appointments = appointmentService.findAllY();
        return Result.success(appointments);
    }

    @GetMapping("/findAllW")
    public Result findAllW(){
        List<Appointment> appointments = appointmentService.findAllW();
        return Result.success(appointments);
    }
}
